#ifndef __THREAD__H
#define __THREAD__H
typedef void*(*ThreadRoutine)(void*);
class Thread
{
public:
	S32 start(ThreadRoutine routine, void *arg) 
	{ return pthread_create(&m_thread, NULL, routine, arg); }
	S32 wait()
	{ void *dummy; return pthread_join(m_thread, &dummy); }
private:
	pthread_t m_thread;

};

class Mutex
{
	friend class ConditionVariable;
public:
	Mutex() { pthread_mutex_init(&m_mutex, NULL); }
	void Lock() { pthread_mutex_lock(&m_mutex); }
	void Unlock() { pthread_mutex_unlock(&m_mutex); }
private:
	pthread_mutex_t m_mutex;
};

class ConditionVariable
{
public:
	ConditionVariable() { pthread_cond_init(&m_cond, NULL); }
	void Wait(Mutex *pMutex) { pthread_cond_wait(&m_cond, &pMutex->m_mutex); }
	void NotifyAll() { pthread_cond_broadcast(&m_cond); }
private:
	pthread_cond_t m_cond;
};

template <class T> 
class ScopeLocker 
{
public:
	ScopeLocker(T *pLock) { (m_pLock = pLock)->Lock(); }
	~ScopeLocker() { m_pLock->Unlock(); }
private:
	T *m_pLock;
};
#endif
